//
//  WBUserAccount.swift
//  SinaWeibo
//
//  Created by zhuangqianwei on 16/11/15.
//  Copyright © 2016年 zhuangqianwei. All rights reserved.
//

import UIKit

class WBUserAccount: NSObject, NSCoding{

    //用户登录的唯一标识
    var access_token: String?
    //token的生命周期
    var expires_in: Double = 0 {
        didSet{
            //一旦设置过期时间 就开始机选过期日期
            expires_date = Date(timeIntervalSinceNow:expires_in)
        }
    }
    //增加过期日期的属性
    var expires_date: Date?
    
    var uid: String?
    //友好显示名称
    var name: String?
    
    //用户头像地址 180*180
    var avatar_large: String?
    
    //自定义构造函数
    init(dict:[String: Any]) {
        super.init()
        setValuesForKeys(dict)
    }
    override func setValue(_ value: Any?, forUndefinedKey key: String) {
        //空实现, 这个方法内部一旦执行super. 就会抛出 Terminating app due to uncaught exception 'NSUnknownKeyException'
    }
    
    //解档 将磁盘中的二进制数据转换为自定义对象
    required init?(coder aDecoder: NSCoder) {
        //给对象的属性赋值
        access_token = aDecoder.decodeObject(forKey: "access_token") as? String
        expires_date = aDecoder.decodeObject(forKey: "expires_date") as? Date
        uid = aDecoder.decodeObject(forKey: "uid") as? String
        name = aDecoder.decodeObject(forKey: "name") as? String
        avatar_large = aDecoder.decodeObject(forKey: "avatar_large") as? String
    }
    
    //归档 将自定义对象 转换为 磁盘中的二进制数据
    func encode(with aCoder: NSCoder) {
        aCoder.encode(access_token, forKey: "access_token")
        aCoder.encode(expires_date, forKey: "expires_date")
        aCoder.encode(uid, forKey: "uid")
        aCoder.encode(name, forKey: "name")
        aCoder.encode(avatar_large, forKey: "avatar_large")
        
    }
    
    
    
}
